package com.sense.sensedialprehandle.utils;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Slf4j
public class RequestUtils {

    public static Map<String,Object> getRequestInfos(HttpServletRequest request){
        Map<String,Object> paramsInfo = getParamsInfo(request);
//        Map headersInfo = getHeadersInfo(request);
//        Map cookieId = getCookieId(request);
//        paramsInfo.put("request headers",headersInfo);
        return paramsInfo;
    }


    /**
     * 解析第三方返回值
     * @param request 请求
     * @return map
     */
    public static Map<String,Object> getParamsInfo(HttpServletRequest request) {
        Map<String,Object> maps = new HashMap<>();
        Map<String,Object> map = new HashMap();
        String uri=request.getRequestURI();
        int port=request.getServerPort();
        Enumeration paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = (String) paramNames.nextElement();
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues.length == 1) {
                String paramValue = paramValues[0];
                if (paramValue.length() != 0) {
                    map.put(paramName, paramValue);
                }else if(paramName.equals("ex")){
                    map.put(paramName, paramValue);
                }
            }
        }
        Map headersInfo = getHeadersInfo(request);
        String ipAddress = getIpAddress(request);
        maps.put("params",map);
        maps.put("url",uri);
        maps.put("ip",ipAddress+":"+port);
        maps.put("request headers",headersInfo);
        return maps;
    }

    private static Map<String, String> getHeadersInfo(HttpServletRequest request) {
        Map<String, String> map = new HashMap<String, String>();
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }
        return map;
    }

    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip;
    }

    private static Map<String, String> getCookieId(HttpServletRequest request){
        Cookie[] cookie = request.getCookies();
        if(cookie==null) return null;
        Map<String, String> cookieMap=new HashMap<>();
        for (int i = 0; i < cookie.length; i++) {
            Cookie cook = cookie[i];
            if(cook.getName().equalsIgnoreCase("xxx")){ //获取键
                System.out.println("account:"+cook.getValue().toString());    //获取值
                String val = cook.getValue().toString();
                cookieMap.put(cook.getName(),val);
            }
        }
        return cookieMap;
    }

}



